/*
 * Created by JFormDesigner on Wed Jun 07 10:19:53 CST 2023
 */

package gui.admin;

import dao.SupplierDao;
import dao.UserDao;
import entity.Supplier;
import entity.User;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 * @author Liubuzhu
 */
public class AddSupplierFrame extends JFrame {
    public AddSupplierFrame() {
        initComponents();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        supplierId = new JTextField();
        supplierName = new JTextField();
        contactNumber = new JTextField();
        address = new JTextField();
        addBtn = new JButton();
        cancelBtn = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u4f9b\u8d27\u5546\u9875\u9762");
        setPreferredSize(new Dimension(560, 500));
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u4f9b\u8d27\u5546\u7f16\u53f7");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label1);
        label1.setBounds(50, 40, 110, 40);

        //---- label2 ----
        label2.setText("\u4f9b\u8d27\u5546\u540d\u79f0");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(50, 115, 110, 40);

        //---- label3 ----
        label3.setText("\u4f9b\u8d27\u5546\u5730\u5740");
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label3);
        label3.setBounds(50, 185, 110, 40);

        //---- label4 ----
        label4.setText("\u8054\u7cfb\u7535\u8bdd");
        label4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label4);
        label4.setBounds(50, 260, 110, 40);
        contentPane.add(supplierId);
        supplierId.setBounds(165, 40, 295, 50);
        contentPane.add(supplierName);
        supplierName.setBounds(165, 115, 295, 50);
        contentPane.add(contactNumber);
        contactNumber.setBounds(165, 185, 295, 50);
        contentPane.add(address);
        address.setBounds(165, 260, 295, 50);

        //---- addBtn ----
        addBtn.setText("\u6dfb\u52a0");
        addBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(addBtn);
        addBtn.setBounds(225, 365, 95, 45);

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String supplierIdTemp = supplierId.getText();
                String supplierNameTemp = supplierName.getText();
                String contactNumberTemp = contactNumber.getText();
                String addressTemp = address.getText();


                Supplier supplier = new Supplier(supplierIdTemp, supplierNameTemp, contactNumberTemp, addressTemp);
                SupplierDao supplierDao = new SupplierDao();

                boolean result = false;
                try {
                    result = supplierDao.addSupplier(supplier);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                if (result) {
                    JOptionPane.showMessageDialog(null, "添加成功");
                } else {
                    JOptionPane.showMessageDialog(null, "添加失败");
                }
            }
        });

        //---- cancelBtn ----
        cancelBtn.setText("\u53d6\u6d88");
        cancelBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(cancelBtn);
        cancelBtn.setBounds(370, 365, 95, 45);

        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dispose();
                try {
                    AdminMainFrame adminMainFrame = new AdminMainFrame();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JTextField supplierId;
    private JTextField supplierName;
    private JTextField contactNumber;
    private JTextField address;
    private JButton addBtn;
    private JButton cancelBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    public static void main(String[] args) {
        AddSupplierFrame addSupplierFrame = new AddSupplierFrame();
    }
}
